package com.longrise.co.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Build;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint({ "SimpleDateFormat", "SdCardPath", "DefaultLocale",
		"HandlerLeak", "WorldWriteableFiles" })
public class PublicUtils {

	public static SimpleDateFormat log_SimpleDateFormat = new SimpleDateFormat(
			"yyyy_MM_dd_HH_mm_ss");
	public static SimpleDateFormat dateSimple = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat dateSimple2 = new SimpleDateFormat("yyyy年MM月dd日  E");
	public static SimpleDateFormat dateSimple3 = new SimpleDateFormat("yyyy-MM-dd");




	/**
	 * Bitmap 转化为序列化的字符
	 */
	public static String Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		return com.longrise.LEAP.Base.IO.JSONSerializer.getInstance()
				.Serialize(baos.toByteArray());
	}

	/**
	 * 获取bitmap的大小
	 */
	@SuppressLint("NewApi")
	public static int getBitmapSize(Bitmap bitmap) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			// API 19
			return bitmap.getAllocationByteCount();
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
			// API 12
			return bitmap.getByteCount();
		}
		return bitmap.getRowBytes() * bitmap.getHeight(); // earlier version
	}

	

	// bitmap转byte[]
	public static byte[] BitmaptoBytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);
		return baos.toByteArray();
	}
	

	/**
	 * 返回系统当前时间 :xxxx年xx月xx日 hh:mm:ss
	 * 
	 * @return
	 */
	public static String getNowTime() {
		try {
			return dateSimple.format(new Date()) + "";
		} catch (Exception e) {
			return null;
		}
	}
	
	public static boolean checkPhone(String str) {
		try {
			boolean flagphone = false;
			if (str.matches("^0?\\d{11}$")) {
				flagphone = true;
			} else if (str.matches("^(\\(?\\d{3,4}[-\\)])?\\d{7,8}$")) {
				flagphone = true;
			}
			return flagphone;
		} catch (Exception e) {
			return false;
		}
	}


}
